package com.ruitu.mall_98_1.product.activity;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.MediaController;
import android.widget.VideoView;

import com.beanu.arad.base.BaseActivity;
import com.beanu.arad.utils.ToastUtils;
import com.ruitu.mall_98_1.R;

//视频播放页面
public class VideoCheckActivity extends BaseActivity {
    private VideoView videoView;

    private String video_url = "";//传过来的视频地址
    private MediaController mediaController;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置全屏,无状态栏
        setContentView(R.layout.activity_video_check);
        videoView = findViewById(R.id.videoView);
        disableSlideBack();

        findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        video_url = getIntent().getExtras().getString("video_url");
        //网络视频
        Uri uri = Uri.parse(video_url);
        mediaController = new MediaController(this);
        videoView.setMediaController(mediaController);//设置视频控制器
        videoView.setVideoURI(uri);//设置视频路径

        showProgressWithText(true, "视频加载中...");

        videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                ToastUtils.showShort("非常抱歉,视频有误");
                finish();
                return false;
            }
        });
        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                showProgress(false);
                videoView.start(); //开始播放视频
            }
        });
        videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                //播放完成回调
                mediaController.show();
            }
        });
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
    }
}
